<?php  
## include required files
/*******************************/
require_once '../model/blogs.php';

/*******************************/
## Create Objects
/*******************************/
$blogObj = new Model_Blogs();
/*******************************/

if(isset($_POST['editblog_btn']) || $_POST['title']) {
	## apply PHP validation for required filed
	if(trim($_POST['title']) != '' && trim($_POST['description']) != '') {
		
			$blogArray = array();
			extract($_POST);
			//********************************************UPlOADING PHOTO*************************************************************//
			if($_FILES['blog_image']['name']!='') {
				
				$size =	getimagesize($_FILES['blog_image']['tmp_name']);
				
				if($_FILES['blog_image']['name']!= '' && ($size[0] >= BLOGIMAGEWIDTH && $size[1] >= BLOGIMAGEHEIGHT) ) {
					
					##unlink the prevoius image
					$blog_image = '../uploads/blog/'.$_POST['hidden_image']; 
					@unlink($blog_image);
					
					$path = '../uploads/blog/';
					$imageName = $_FILES['blog_image']['name'];
					$arrImageName = explode(".",$imageName);
					$position = count($arrImageName);
					$fileName = date('Ymdhis').".".$arrImageName[$position-1];
					copy($_FILES["blog_image"]["tmp_name"],$path.$fileName);
					$blogArray['image'] = $fileName;
				} else {
					$_SESSION['msg'] = "<div class='error_msg'><span>".BLOGIMAGE_WIDTHHEIGHT."</span></div>";
				}	
			}
			
			if($_SESSION['msg'] =='') {
				$blog_id = base64_decode($_GET['id']);
				$blogArray['title'] = return_post_value($_POST['title']);
				$blogArray['description'] = return_post_value($description);
				$blogArray['introtext'] = return_post_value($introtext);
				$blogArray['user_id'] = 1;
				$blogArray['modified_date'] = date('Y-m-d');
				$blogObj->editBlogById($blogArray, $blog_id);
				$_SESSION['msg'] = "<div class='success_msg'><span>Blog edited successfully</span></div>";
				header('location:'.SITE_URL.'/admin/home.php?q=blogs&page='.$_POST['page']);
				exit;
			}
      	 //************************************************************************************************************
	} else {// php validation else
		$_SESSION['msg'] = "<div class='error_msg'><span>Please enter Blog title and Description</span></div>";
	}
	// main if closed
}

if($_GET['id']!=''&& $_GET['action']=='edit') {
	$blog_id = base64_decode($_GET['id']);
	$blogArray = $blogObj->getBlogDetailsById($blog_id);
	$smarty->assign('blogArray', $blogArray);
}

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'blogs');
$smarty->assign('mainmenu', '3');
## Unset all the objects created which are on this page
unset($blogObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'blogs/editblog.tpl');
unset($smarty);
?>